package com.scada.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scada.dao.DictionaryDataDao;
import com.scada.inf.DictionaryDataVisitor;
import com.scada.model.bean.DictionaryData;
import com.scada.model.bean.vo.DictionaryVO;
import com.scada.model.common.StateEnum;
import com.scada.model.result.ActionResult;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;





@Service("dictionaryDataVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class DictionaryDataVisitorImpl implements DictionaryDataVisitor {
	
	@Autowired
	private DictionaryDataDao dictionaryDataDao;

	@Override
	public ActionResult deleteByPrimaryKey(Map<String, Object> map) {
		ActionResult result = new ActionResult();
		List<String> msgList=new ArrayList<String>();
		int i = dictionaryDataDao.deleteByPrimaryKey(map);
		if(i > 0 ){
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			msgList.add("删除失败");
			result.setMessages(msgList);
		}
		return result;
	}

	@Override
	public ActionResult insertSelective(DictionaryData record) {
		ActionResult result = new ActionResult();
		List<String> msgList=new ArrayList<String>();
		if(dictionaryDataDao.selectByName(record)!=null||dictionaryDataDao.selectByType(record)!=null){
			result.setSuccess(false);
			if(dictionaryDataDao.selectByName(record)!=null){
				msgList.add("名称已存在");
			}
			if(dictionaryDataDao.selectByType(record)!=null){
				msgList.add("编码已存在");
			}
			result.setMessages(msgList);
			return result;
		}
		int i = dictionaryDataDao.insertSelective(record);
		if(i > 0 ){
			result.setSuccess(true);
		}else{
			result.setSuccess(false);
			msgList.add("新增失败");
			result.setMessages(msgList);
		}
		return result;
	}

	@Override
	public DictionaryData selectByPrimaryKey(Map<String, Object> map) {
		
		return dictionaryDataDao.selectByPrimaryKey(map);
	}

	@Override
	public ActionResult updateByPrimaryKeySelective(DictionaryData dictionaryData) {
		ActionResult result = new ActionResult();
		
		List<String> msgList=new ArrayList<String>();
		
		int msg = dictionaryDataDao.onlyVer(dictionaryData);
		
		if(msg>0){	
			msgList.add("数据重复");
			result.setSuccess(false);
			result.setMessages(msgList);
		}else{
			 dictionaryDataDao.updateByPrimaryKeySelective(dictionaryData);
			 result.setSuccess(true);
			 msgList.add("更新成功");
			 result.setMessages(msgList);
		}
		return result;
	}

	@Override
	public PageInfo<DictionaryVO> selectAll(PageInfo<DictionaryVO> pageInfo) {
		DictionaryVO  dictionaryData =(DictionaryVO) pageInfo.getCondition();
		int count = dictionaryDataDao.count(dictionaryData);
		pageInfo.setRecordsTotal(count);
		List<DictionaryVO> list =dictionaryDataDao.selectAll(pageInfo);
		pageInfo.setData(list);
		return pageInfo;
	}

	@Override
	public ResponseBean searchDictionaryDataByType(String type) {
		
		ResponseBean rb=new ResponseBean();
		try{
			List<DictionaryData> dataList=dictionaryDataDao.searchDictionaryDataByType(type);
			rb.getResultMap().put("resultData", dataList);
			rb.success();
		}catch(Exception e){
			ByteArrayOutputStream baos = new ByteArrayOutputStream();  
			e.printStackTrace(new PrintStream(baos));  
			String exception = baos.toString();  
			rb.failed(exception);
		}
		return rb;
	}

	@Override
	public List<DictionaryData> getDataByType(String type) {
		
		Map<String ,Object> map = new HashMap<String, Object>();
		map.put("state", StateEnum.State.STATE_SHOW.getKey());
		map.put("type", type);
		return dictionaryDataDao.getDataByType(map);
	}

	@Override
	public List<DictionaryData> findempJobName() {

		return dictionaryDataDao.findempJobName();
	}

	@Override
	public List<DictionaryData> findDictionaryType(String type) {
		
		return dictionaryDataDao.findDictionaryType(type);
	}

	@Override
	public DictionaryData getDataByTypeAndKey(String type, String typeKey) {
		
		Map<String ,Object> map = new HashMap<String, Object>();
		map.put("state", StateEnum.State.STATE_SHOW.getKey());
		map.put("type", type);
		map.put("typeKey", typeKey);
		return dictionaryDataDao.getDataByTypeAndKey(map);
	}


	@Override
	public ResponseBean getSelectDicPageDate(Integer pageSize, Integer currentPage, String searchName,
			String dicKey) {
		
		ResponseBean rb=new ResponseBean();
		try{
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("searchName", searchName);
			map.put("typeCode", dicKey);
			rb.addObject("resultCount", dictionaryDataDao.getSelectDicDataCount(map));
			map.put("start", currentPage*pageSize);
			map.put("pageSize", pageSize);
			rb.addObject("resultList", dictionaryDataDao.getSelectDicDataPageList(map));
			rb.success();
		}catch(Exception e){
			rb.failed(e.getMessage());
		}
		return rb;
	}


	

	

}
